package com.supermarket_project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supermarket_project.common.R;
import com.supermarket_project.mapper.CategoryMapper;
import com.supermarket_project.pojo.Category;
import com.supermarket_project.pojo.CategoryVo;
import com.supermarket_project.pojo.Goods;
import com.supermarket_project.service.CategoryService;
import com.supermarket_project.service.GoodsService;
import com.supermarket_project.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private GoodsService goodsService;

    @Override
    public R addCategory(Category category) {
        List<Category> categoryList = selectCategory();
        for (Category c : categoryList) {
            if (c.getCategoryName().equals(category.getCategoryName())){
                return R.error("该类别已存在，请勿重复添加");
            }
        }
        categoryMapper.addCategory(category);
        if (category.getFatherId() == null){
            return R.success(null,"添加一级类别成功！");
        }
        return R.success(null,"添加二级类别成功！");
    }

    @Override
    public List<Category> selectCategory() {
        //查询所有类别名称
        return categoryMapper.selectCategory();
    }

    @Override
    public R primaryCategory() {
        //查询所有一级商品
        List<Category> primaryCategoryList = categoryMapper.primaryCategory();
        return R.success(primaryCategoryList,"操作成功");
    }

    @Override
    public R pageCategory(int pageNum, int pageSize, Integer fatherId, String primary, String second) {
        PageHelper.startPage(pageNum, pageSize);
        List<Category> categoryList = categoryMapper.pageCategory(pageNum,pageSize,fatherId,primary,second);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList,5);
        List<Category> pageInfoList = pageInfo.getList();
        List<Category> categories = pageInfoList.stream()
                .map(category -> {
                    if (category.getFatherId() == null) {
                        category.setFatherName("无");
                    } else {
                        Category queryCategory = categoryMapper.queryCategory(category.getFatherId());
                        category.setFatherName(queryCategory.getCategoryName());
                    }
                    return category;
                }).collect(Collectors.toList());
        pageInfo.setList(categories);
        return R.success(pageInfo,"操作成功");
    }

    @Override
    public R secondCategory(Integer categoryId) {
        //查询所有一级商品
        List<Category> secondCategoryList = categoryMapper.secondCategory(categoryId);
        return R.success(secondCategoryList,"操作成功");
    }

    @Override
    public R updateCategory(Category category) {
        categoryMapper.updateCategory(category);
        return R.success(null,"修改成功");
    }

    @Override
    public R queryCategory(Integer categoryId) {
        Category category = categoryMapper.queryCategory(categoryId);
        return R.success(category,"操作成功");
    }

    @Override
    public R secondCategoryByPrimary(int fatherId) {
        List<Category> categoryList = categoryMapper.secondCategoryByPrimary(fatherId);
        return R.success(categoryList,"操作成功");
    }

    @Override
    @Transactional
    public R lockCategory(int categoryId, int status) {
        categoryMapper.lockCategory(categoryId,status);
        Category category = categoryMapper.queryCategory(categoryId);
        if (category.getFatherId() == null){
            List<Category> secondCategoryList = categoryMapper.queryCategoryByFatherId(category.getCategoryId());
            for (Category secondCategory : secondCategoryList) {
                categoryMapper.lockCategory(secondCategory.getCategoryId(), status);
                System.out.println(secondCategory.getCategoryId());
                List<Goods> goodsList = goodsService.queryGoodsByCategoryId(secondCategory.getCategoryId());
                if (goodsList != null){
                    for (Goods goods : goodsList) {
                        goodsService.lockGoods(goods.getGoodsId(), status);
                    }
                }
            }
            if (status == 1){
                return R.success(null,"取消该一级分类锁定成功");
            }
            return R.success(null,"锁定该一级分类成功");
        }
        List<Goods> goodsList = goodsService.queryGoodsByCategoryId(category.getCategoryId());
        if(goodsList != null){
            for (Goods goods : goodsList) {
                goodsService.lockGoods(goods.getGoodsId(), status);
            }
        }
        if (status == 1){
            return R.success(null,"取消该二级分类锁定成功");
        }
        return R.success(null,"锁定该二级分类成功");
    }

    @Override
    public R allCategory() {
        List<Category> primaryCategoryList = categoryMapper.primaryCategory();
        List<CategoryVo> categoryVoList = BeanCopyUtils.copyBeanList(primaryCategoryList, CategoryVo.class);
        List<CategoryVo> categoryVos = categoryVoList.stream()
                .map(categoryVo -> {
                    List<Category> categoryList = categoryMapper.queryCategoryByFatherId(categoryVo.getCategoryId());
                    categoryVo.setChildren(categoryList);
                    return categoryVo;
                }).collect(Collectors.toList());
        return R.success(categoryVos,"操作成功");
    }
}
